﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;//添加命名空间，可以使用OleDbConnection等对象
using System.Data;//添加命名空间，可以使用dataset对象

public partial class adminstrator_editTeacher : System.Web.UI.Page
{
    public OleDbConnection myConnection;
    public static readonly string sConnectionString = System.Configuration.ConfigurationManager.AppSettings["SQLConnString"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        myConnection = new OleDbConnection(sConnectionString);
        myConnection.Open();//打开数据库连接
        if (Session["UserName"] == null && Session["UserNo"] == null)//没有经过正常连接途径打开页面
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>" +
                "alert('登录超时（或未登录），请您重新登陆！');parent.location.herf='../login.aspx'</script>", false);
        if (!IsPostBack)//page.IsPostBack是一个标志，当前请求是否第一次打开，IsPostBack只有第一次打的时候是false,其他时候是true
        {
            string detID = Request.QueryString["deleteID"];//获取页面传递的ID号
            if (detID != null)
            {

                GridView1.PageIndex = int.Parse(Request.QueryString["pIndex"]);//获取页面传递的显示页索引
                delete(detID);//调用删除函数
            }
        }
    }



    public void bind(string seField, string seValue)
    {

        string mysql = "select * from TeacherTb   where   " + seField + "   like   '%" + seValue + "%' order by ID";
        OleDbCommand mycmd = new OleDbCommand(mysql, myConnection);
        OleDbDataAdapter Adapter = new OleDbDataAdapter();
        Adapter.SelectCommand = mycmd;
        DataSet myDS = new DataSet();
        Adapter.Fill(myDS);
        if (myDS.Tables.Count > 0 && myDS.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = myDS;
            GridView1.DataKeyNames = new string[] { "tNo" };
            GridView1.DataBind();
            Label1.Text = "";
        }
        else
        {
            Label1.Text = "无法查到相关数据！";
            GridView1.DataSource = null;//装载空数据源
            GridView1.DataBind();//绑定数据到显示列表
        }
    }
    public void delete(string myNo)/*删除函数*/
    {
        string mystr = "delete from TeacherTb where tNo='" + myNo + "'";
        OleDbCommand mycmd = new OleDbCommand(mystr, myConnection);// OleDbCommand表示要对access数据源执行的sql语句或存储过程
        mycmd.ExecuteNonQuery();//执行SQL语句
        GridView1.EditIndex = -1;//把要编辑的行语句的索引设置为-1
        //修改ScoreTb表，将信息中的工号，姓名填空
        mystr = "Update ScoreTb set tNo ='',+ tName +''where tNo='" + myNo + "'";//更新成绩表
        mycmd = new OleDbCommand(mystr, myConnection);
        mycmd.ExecuteNonQuery();
        //修改CourseTb表，将信息中的工号，姓名填空
        mystr = "Update CourseTb set tNo ='',+ tName +''where tNo='" + myNo + "'";//更新课程表
        mycmd = new OleDbCommand(mystr, myConnection);
        mycmd.ExecuteNonQuery();
        //修改SelcourseTb表，将信息中的工号，姓名填空
        mystr = "Update SelcourseTb set tNo ='',+ tName +''where tNo='" + myNo + "'";//更新选课表
        mycmd = new OleDbCommand(mystr, myConnection);
        mycmd.ExecuteNonQuery();
        //调用数据绑定函数，将需要查询的字段和输入的查询内容作为参数传递
        bind(Session["sDL"].ToString(), Session["sTB"].ToString());
        /*将session的值回传给列表框和输入框*/
        TextBox1.Text = Session["sTB"].ToString();
        if (Session["sDL"].ToString() == "tNo")
            DropDownList1.SelectedIndex = 0;
        else if (Session["sDL"].ToString() == "tName")
            DropDownList1.SelectedIndex = 1;
        else
            DropDownList1.SelectedIndex = 3;


    }
    protected void ButtonOK_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != "")
        {
            Session["sDL"] = "";
            Session["sTB"] = TextBox1.Text.Trim();
            if (DropDownList1.SelectedIndex == 0)
                Session["sDL"] = "tNo";
            else if (DropDownList1.SelectedIndex == 1)
                Session["sDL"] = "tName";
            else if (DropDownList1.SelectedIndex == 2)
                Session["sDL"] = "tSex";
            else if (DropDownList1.SelectedIndex == 3)
                Session["sDL"] = "tDepartment";
            else
                Session["sDL"] = "sClass";
            bind(Session["sDL"].ToString(), Session["sTB"].ToString());
        }

    }
    protected void ButtonReset_Click(object sender, EventArgs e)
    {
        DropDownList1.SelectedIndex = 0;
        TextBox1.Text = "";
        Label1.Text = "";
        GridView1.DataSource = null;
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;//获取所编辑的行的索引
        bind(Session["sDL"].ToString(), Session["sTB"].ToString());//调用数据绑定函数，将需要查询的字段和输入的查询内容作为参数传递

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string stuID = GridView1.DataKeys[e.NewEditIndex][0].ToString();
        //跳转到编辑学生页面，同时将参数ID传递到编辑页面（相当于重新打开网页）
        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>location.herf='editStudentStart.aspx?stuID=" + stuID + "';</script>", false);
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>" +
            "if(confirm('确定删除该老师的信息?')){location.href='editTeacher.aspx?deleteID=" + GridView1.DataKeys[e.RowIndex][1].ToString() + "&pIndex=" + GridView1.PageIndex.ToString() + "'}</Script>", false);

    }
}